package com.java110.mall.cart.smo.impl;


import com.java110.mall.cart.dao.IHousekeepingServPoolUserServiceDao;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.housekeeping.HousekeepingServPoolUserDto;

import com.java110.intf.cart.IHousekeepingServPoolUserInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.po.housekeeping.HousekeepingServPoolUserPo;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.core.base.smo.BaseServiceSMO;
import com.java110.dto.PageDto;
import com.java110.utils.util.DateUtil;
import com.java110.vo.api.junkRequirement.PhotoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName FloorInnerServiceSMOImpl
 * @Description 工单流转内部服务实现类
 * @Author wuxw
 * @Date 2019/4/24 9:20
 * @Version 1.0
 * add by wuxw 2019/4/24
 **/
@RestController
public class HousekeepingServPoolUserInnerServiceSMOImpl extends BaseServiceSMO implements IHousekeepingServPoolUserInnerServiceSMO {

    @Autowired
    private IHousekeepingServPoolUserServiceDao housekeepingServPoolUserServiceDaoImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;


    @Override
    public int saveHousekeepingServPoolUser(@RequestBody HousekeepingServPoolUserPo housekeepingServPoolUserPo) {
        int saveFlag = 1;
        housekeepingServPoolUserServiceDaoImpl.saveHousekeepingServPoolUserInfo(BeanConvertUtil.beanCovertMap(housekeepingServPoolUserPo));
        return saveFlag;
    }

     @Override
    public int updateHousekeepingServPoolUser(@RequestBody  HousekeepingServPoolUserPo housekeepingServPoolUserPo) {
        int saveFlag = 1;
         housekeepingServPoolUserServiceDaoImpl.updateHousekeepingServPoolUserInfo(BeanConvertUtil.beanCovertMap(housekeepingServPoolUserPo));
        return saveFlag;
    }

     @Override
    public int deleteHousekeepingServPoolUser(@RequestBody  HousekeepingServPoolUserPo housekeepingServPoolUserPo) {
        int saveFlag = 1;
        housekeepingServPoolUserPo.setStatusCd("1");
        housekeepingServPoolUserServiceDaoImpl.updateHousekeepingServPoolUserInfo(BeanConvertUtil.beanCovertMap(housekeepingServPoolUserPo));
        return saveFlag;
    }

    @Override
    public List<HousekeepingServPoolUserDto> queryHousekeepingServPoolUsers(@RequestBody  HousekeepingServPoolUserDto housekeepingServPoolUserDto) {

        //校验是否传了 分页信息

        int page = housekeepingServPoolUserDto.getPage();

        if (page != PageDto.DEFAULT_PAGE) {
            housekeepingServPoolUserDto.setPage((page - 1) * housekeepingServPoolUserDto.getRow());
        }

        List<HousekeepingServPoolUserDto> housekeepingServPoolUsers = BeanConvertUtil.covertBeanList(housekeepingServPoolUserServiceDaoImpl.getHousekeepingServPoolUserInfo(BeanConvertUtil.beanCovertMap(housekeepingServPoolUserDto)), HousekeepingServPoolUserDto.class);

        refreshPoolUser(housekeepingServPoolUsers);
        return housekeepingServPoolUsers;
    }

    private void refreshPoolUser(List<HousekeepingServPoolUserDto> housekeepingServPoolUsers) {
        long duration = 0;
        for (HousekeepingServPoolUserDto housekeepingServPoolUser : housekeepingServPoolUsers) {
            if (housekeepingServPoolUser.getEndTime() == null) {
                duration = DateUtil.getCurrentDate().getTime() - housekeepingServPoolUser.getStartTime().getTime();
            } else {
                duration = housekeepingServPoolUser.getEndTime().getTime() - housekeepingServPoolUser.getStartTime().getTime();
            }
            housekeepingServPoolUser.setDuration(getCostTime(duration));
        }

        //刷入图片信息
        List<PhotoVo> photoVos = null;  //业主上传维修图片
        PhotoVo photoVo = null;
        for (HousekeepingServPoolUserDto housekeepingServPoolUser : housekeepingServPoolUsers) {
            FileRelDto fileRelDto = new FileRelDto();
            fileRelDto.setObjId(housekeepingServPoolUser.getPoolId());
            List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
            photoVos = new ArrayList<>();
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (tmpFileRelDto.getRelTypeCd().equals("14000")) {  //维修图片
                    photoVo = new PhotoVo();
                    photoVo.setUrl("/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=" + housekeepingServPoolUser.getShopId());
                    photoVo.setRelTypeCd(tmpFileRelDto.getRelTypeCd());
                    photoVos.add(photoVo);
                } else {
                    continue;
                }
            }
            housekeepingServPoolUser.setPhotoVos(photoVos);
        }
    }


    public String getCostTime(Long time) {
        if (time == null) {
            return "00:00";
        }
        long hours = time / (1000 * 60 * 60);
        long minutes = (time - hours * (1000 * 60 * 60)) / (1000 * 60);
        String diffTime = "";
        if (minutes < 10) {
            diffTime = hours + ":0" + minutes;
        } else {
            diffTime = hours + ":" + minutes;
        }
        return diffTime;
    }


    @Override
    public int queryHousekeepingServPoolUsersCount(@RequestBody HousekeepingServPoolUserDto housekeepingServPoolUserDto) {
        return housekeepingServPoolUserServiceDaoImpl.queryHousekeepingServPoolUsersCount(BeanConvertUtil.beanCovertMap(housekeepingServPoolUserDto));    }

    public IHousekeepingServPoolUserServiceDao getHousekeepingServPoolUserServiceDaoImpl() {
        return housekeepingServPoolUserServiceDaoImpl;
    }

    public void setHousekeepingServPoolUserServiceDaoImpl(IHousekeepingServPoolUserServiceDao housekeepingServPoolUserServiceDaoImpl) {
        this.housekeepingServPoolUserServiceDaoImpl = housekeepingServPoolUserServiceDaoImpl;
    }
}
